<template>
    <div class="login-wrap">
        <div class="ms-title">贝颜后台管理系统</div>
        <div class="ms-login">
            <el-form :model="ruleForm"  ref="ruleForm" label-width="0px" class="demo-ruleForm">
                <el-form-item prop="username">
                    <el-input v-model="ruleForm.username" placeholder="请输入手机号"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="请输入密码" v-model="ruleForm.password" @keyup.enter.native="submitForm('ruleForm')"></el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" refs="login" @click="login('ruleForm')">登录</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
import {
    ajaxGet,
    storeAsGet,
    storeAsPost,
    logining
} from '../api/api'
import CryptoJS from 'crypto-js/crypto-js'
var reMobile = /^1[3-9]\d{9}$/

export default {
    data: function(){
        return {
            ruleForm: {
                username: '',
                password: ''
            }
        }
    },
    created(){
        if(localStorage.getItem('token')){
            this.$router.push('/order');
        }
    },
    methods: {
        check() {
            if (!String.trim(this.ruleForm.username)) {
                 this.$message.error('请输入手机号');
                return false
            }
            if (!String.trim(this.ruleForm.password)) {
                this.$message.error('请输入密码');
                return false
            }
            if (!reMobile.test(this.ruleForm.username)) {
                 this.$message.error('请输入正确的手机号');
                return false
            }
            return true
		},
        login() {
			if (!this.check()) {
			    return
			}
             let shaPassword = CryptoJS.SHA1(this.ruleForm.password).toString()//加密
        //     logining({
        //         mobileNumber: this.ruleForm.username,
        //         sha1password: shaPassword
        //     }).then(res => {
        //         console.log(res)
        //         console.log('请求成功')
        //         localStorage.setItem('token',res.data.content.access_token);
        //         localStorage.setItem('username',this.ruleForm.username);
        //         this.$router.push('/');
        //    }).catch(res => {
        //        console.log('请求失败')
        //         this.$message.error(res.response.data.detail);
        //     })
            let _this = this;
            storeAsPost({},'token/password?mobileNumber='+_this.ruleForm.username+'&sha1password='+shaPassword).then(res=>{
                console.log(res,"444")
                localStorage.setItem('token',res.data.content.access_token);
                localStorage.setItem('username',_this.ruleForm.username);
                localStorage.setItem('sysUserName', res.data.content.userName)
                localStorage.removeItem('userRole');
                ajaxGet('/role/user/relation/userId').then(function(res){
                    console.log(res.data.content.role,'这里是用户组')
                    localStorage.setItem('roleName',res.data.content.role.roleName);
                    localStorage.setItem('userRole',res.data.content.role.roleInfo);
                    console.log(localStorage.getItem('userRole'),'取没取到缓存')
                });


                _this.$router.push('/');
            }).catch(errors=>{
                this.$message.error(errors.response.data.detail)
            });
        }
    }
}
</script>

<style scoped>
    .login-wrap{
        position: relative;
        width:100%;
        height:100%;
    }
    .ms-title{
        position: absolute;
        top:50%;
        width:100%;
        margin-top: -230px;
        text-align: center;
        font-size:30px;
        color: #fff;

    }
    .ms-login{
        position: absolute;
        left:50%;
        top:50%;
        width:300px;
        height:160px;
        margin:-150px 0 0 -190px;
        padding:40px;
        border-radius: 5px;
        background: #fff;
    }
    .login-btn{
        text-align: center;
    }
    .login-btn button{
        width:100%;
        height:36px;
    }
</style>
